package com.college.heyuhua.controller;

import com.college.heyuhua.model.EmailInfo;
import com.college.heyuhua.service.IEmailService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Created by 赵东坡 on 2019/2/5.
 * Time:16:57
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Controller
public class EmailController {

    @Autowired
    private IEmailService iEmailService;


    @RequestMapping("/insertEmail")
    public String insertEmail(@RequestParam("name") String name,
                              @RequestParam("email") String email,
                              @RequestParam("dept") String dept,
                              @RequestParam("message") String message, HttpServletResponse response){

        boolean flag = false;

        EmailInfo emailInfo = new EmailInfo();
        emailInfo.setDept(dept);
        emailInfo.setEmail(email);
        emailInfo.setMessage(message);
        emailInfo.setName(name);

        flag = iEmailService.insertEmail(emailInfo);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter writer = null;
        try{
            writer = response.getWriter();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        if(flag) {
            writer.print(flag?"发送成功":"发送失败");
            writer.close();
            writer.flush();
        }

        return "404";
    }
}
